<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/9/21
 * Time: 7:38 PM
 */

namespace app\api\controller\v1;

use app\api\model\ProductOrder;
use app\api\model\Store as StoreModel;
use app\api\model\StoreUserItem;
use app\api\model\TeamUser as TeamUserModel;
use app\api\model\User as UserModel;
use app\api\model\UserInvitation;
use app\api\model\UserMoney as UserMoneyModel;
use app\api\service\Token as TokenService;
use app\api\model\StoreUser as StoreUserModel;

class OrderRank
{
    // 获取当前店员下级订单量查询
    public function getOrderRankList()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)->find();
        if(!$storeUser) {
            return [];
        }
        if($storeUser['type'] == 1 || $storeUser['type'] == 4) {
            // 老板
            if($storeUser['type'] == 4) {
                $storeUserItem = StoreUserItem::where('store_user_id',$storeUser['id'])->select()->toArray();
                $storeIds = array_column($storeUserItem,'store_id');
                $orderList = ProductOrder::where('store_id','in',$storeIds)
                    ->where('status','gt','1')->order('id desc')
                    ->with('user,content')
                    ->select()->toArray();
            } else {
                // 店长
                $orderList = ProductOrder::where('store_id','eq',$storeUser['store_id'])
                    ->where('status','gt','1')->order('id desc')
                    ->with('user,content')
                    ->select()->toArray();
            }
            foreach ($orderList as $k=>$v){
                $invite = UserInvitation::where('invitee',$orderList[$k]['user_id'])->find();
                if($invite['type'] == 1) {
                    $orderList[$k]['invite'] = UserModel::get($invite['inviter']);
                }elseif($invite['type'] == 2){
                    $store_user = StoreUserModel::get($invite['inviter']);
                    $orderList[$k]['invite'] = UserModel::get($store_user['user_id']);
                    $orderList[$k]['invite']['nickname'] = $store_user['name'];
                    $orderList[$k]['invite']['phone'] = $store_user['phone'];
                }elseif($invite['type'] == 3){
                    $team_user = TeamUserModel::get($invite['inviter']);
                    $orderList[$k]['invite'] = UserModel::get($team_user['user_id']);
                    $orderList[$k]['invite']['nickname'] = $team_user['name'];
                    $orderList[$k]['invite']['phone'] = $team_user['phone'];
                }
                $orderList[$k]['pay_time_int'] = strtotime($orderList[$k]['pay_time']);
            }
            $storeUser['order']  = $orderList;
            // 店长
        } elseif($storeUser['type'] == 2){
            // 组长
            $storeUsers = StoreUserModel::where('group_id',$storeUser['group_id'])
                ->with('user')->select()->toArray();
            $orderList = [];
            foreach ($storeUsers as $k=>$v){
                $userInvitation = UserInvitation::where('type','eq',2)
                    ->where('inviter','eq',$v['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitation,'invitee');
                $order = ProductOrder::where('user_id','in',$ids)
                    ->where('status','gt',1)
                    ->with('user,content')
                    ->select();
                if(!$order->isEmpty()) {
                    foreach ($order->toArray() as $vo){
                        array_push($orderList,$vo);
                    }
                }
            }
            $storeUser['order']  = $orderList;
        }elseif($storeUser['type'] == 3) {
            // 店员
            $whereTwo[] = ['inviter','eq',$storeUser['id']];
            $whereTwo[] = ['type','eq',2];
            $userInvitation = UserInvitation::where($whereTwo)->select()->toArray();
            $ids = array_column($userInvitation,'invitee');
            $whereThree[] = ['user_id','in',$ids];
            $whereThree[] = ['status','gt',1];
            $order = ProductOrder::where($whereThree)
                ->with('user,content')->select()->toArray();
            $storeUser['order'] = $order;
        }
        return $storeUser;
    }


    public function getOrderTeamRankList($user_id)
    {
        $uid = TokenService::getCurrentUid();
        if(!$user_id) {
            $teamUser = TeamUserModel::where('user_id',$uid)->find();
            $userInvitation = UserInvitation::where('type',3)
                ->where('inviter',$teamUser['id'])
                ->with('user')
                ->select()->toArray();
            $ids = array_column($userInvitation,'invitee');
            array_push($ids,$uid);
            return ProductOrder::where('user_id','in',$ids)
                ->where('status','gt',1)
                ->with('user,content')
                ->select()->toArray();
        } else {
            return ProductOrder::where('user_id','eq',$uid)
                ->where('status','gt',1)
                ->with('user,content')
                ->select()->toArray();
        }
    }


    // 总管理 - 按照门店查询订单数量
    public function getOrderNumByStore($start_time,$end_time)
    {
        $where = [];
        if(!$start_time){
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        $where[] = ['status','gt',1];
        $store = StoreModel::order('id desc')->select()->toArray();
        $num = 0;
        foreach ($store as $k=>$v){
            $whereOne = $where;
            $whereOne[] = ['store_id','eq',$v['id']];
            $store[$k]['order_number'] = ProductOrder::where($whereOne)->count();
            $num += $store[$k]['order_number'];
        }
        if(count($store) > 0) {
            $store = arraySort($store,'order_number');
        }
        return [
            'data' => $store,
            'sum' => $num
        ];
    }

    // 总管理 - 按照门店订单详情查询
    public function getOrderListByStore($store_id,$start_time,$end_time)
    {
        $where = [];
        if(!$start_time){
            $start_time = strtotime("-3 day");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time." 00:00:00")];
            $where[] = ['pay_time', 'elt', strtotime($end_time." 23:59:59")];
        }
        $where[] = ['status','gt',1];
        if($store_id) {
            $where[] = ['store_id','eq',$store_id];
        }
        $array = [];
        $number = 0;
        $list =  ProductOrder::where($where)
            ->with('user,store')
            ->order('pay_time desc')
            ->select()->toArray();
        foreach ($list as $k=>$v){
            $invite = UserInvitation::where('invitee',$list[$k]['user_id'])->find();
            if($invite['type'] == 1) {
                $list[$k]['invite'] = UserModel::get($invite['inviter']);
            }elseif($invite['type'] == 2){
                $store_user = StoreUserModel::get($invite['inviter']);
                $list[$k]['invite'] = UserModel::get($store_user['user_id']);
                $list[$k]['invite']['nickname'] = $store_user['name'];
                $list[$k]['invite']['phone'] = $store_user['phone'];
            }elseif($invite['type'] == 3){
                $team_user = TeamUserModel::get($invite['inviter']);
                $list[$k]['invite'] = UserModel::get($team_user['user_id']);
                $list[$k]['invite']['nickname'] = $team_user['name'];
                $list[$k]['invite']['phone'] = $team_user['phone'];
            }
            array_push($array,$list[$k]);
            $number++;
        }

        return [ 'data'=>$array,'sum' =>$number ];
    }
}